<?php
namespace App\Wen\Utils;
use DateTime;

class StrUtils{

    public static function limit($string, $limit = 20, $dian = true, $is_mb = true){
        if($is_mb){
            if(mb_strlen($string) > $limit){
                if($dian){
                    return mb_substr($string, 0, $limit - 3, "UTF-8") . '...';
                }else{
                    return mb_substr($string, 0, $limit, "UTF-8");
                }
            }
        }
        return $string;
    }

    public static function is_phone_number($string){
        if(_empty_($string)){
            return false;
        }
        $pattern = '/^1[3456789]\d{9}$/';
        return preg_match($pattern, $string);
    }

    public static function is_contain_chinese($string) {
        if(_empty_($string)){
            return false;
        }
        if (preg_match('/[\x{4e00}-\x{9fa5}]+/u', $string)) {
            return true; // 字符串包含汉字
        } else {
            return false; // 字符串不包含汉字
        }
    }


    public static function is_valid_date($date, $format = 'Y-m-d'){
        $d = DateTime::createFromFormat($format, $date);
        return $d && $d->format($format) === $date;
    }


    public static function age($date, $format = 'Y-m-d'){
        if($format == 'timestamp'){
            $date = timetostr($date, 'Y-m-d');
            $format = 'Y-m-d';
        }
        if(self::is_valid_date($date, $format)){
            $today = date('Y-m-d'); // 当前日期
            $diff = date_diff(date_create($date), date_create($today));
            $years = $diff->format('%y');
            $months = $diff->format('%m');
            $days = $diff->format('%d');

            if ($years < 1 && $months < 1) {
                $age = $days . "天";
            } elseif ($years < 1) {
                $age = $months . "个月";
            } else {
                $age = $years . "岁";
            }

// 输出年龄
            return $age;
        }
        return '';
    }


    public static function constellation($date, $format = 'Y-m-d'){
        if(self::is_valid_date($date, $format)){
            // 将日期字符串转换为日期对象
            $date = new DateTime($date);
// 获取月份和日期
            $month = $date->format('m');
            $day = $date->format('d');
// 根据月份和日期判断星座
            if (($month == 1 && $day >= 20) || ($month == 2 && $day <= 18)) {
                $constellation = '水瓶座';
            } elseif (($month == 2 && $day >= 19) || ($month == 3 && $day <= 20)) {
                $constellation = '双鱼座';
            } elseif (($month == 3 && $day >= 21) || ($month == 4 && $day <= 19)) {
                $constellation = '白羊座';
            } elseif (($month == 4 && $day >= 20) || ($month == 5 && $day <= 20)) {
                $constellation = '金牛座';
            } elseif (($month == 5 && $day >= 21) || ($month == 6 && $day <= 21)) {
                $constellation = '双子座';
            } elseif (($month == 6 && $day >= 22) || ($month == 7 && $day <= 22)) {
                $constellation = '巨蟹座';
            } elseif (($month == 7 && $day >= 23) || ($month == 8 && $day <= 22)) {
                $constellation = '狮子座';
            } elseif (($month == 8 && $day >= 23) || ($month == 9 && $day <= 22)) {
                $constellation = '处女座';
            } elseif (($month == 9 && $day >= 23) || ($month == 10 && $day <= 22)) {
                $constellation = '天秤座';
            } elseif (($month == 10 && $day >= 23) || ($month == 11 && $day <= 21)) {
                $constellation = '天蝎座';
            } elseif (($month == 11 && $day >= 22) || ($month == 12 && $day <= 21)) {
                $constellation = '射手座';
            } else {
                $constellation = '摩羯座';
            }
            return $constellation;
        }
        return '';
    }

    /**
     * Determine if a given string ends with a given substring.
     *
     * @param string|array $needles
     */
    public static function endsWith(string $haystack, $needles): bool
    {
        foreach ((array) $needles as $needle) {
            if (mb_substr($haystack, -mb_strlen($needle)) === (string) $needle) {
                return true;
            }
        }

        return false;
    }

    /**
     * Determine if a given string starts with a given substring.
     *
     * @param string|array $needles
     */
    public static function startsWith(string $haystack, $needles): bool
    {
        foreach ((array) $needles as $needle) {
            if ('' !== $needle && mb_substr($haystack, 0, mb_strlen($needle)) === (string) $needle) {
                return true;
            }
        }

        return false;
    }

}
